package com.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dao.ShangpinxinxiDao;
import com.dao.StoreupDao;
import com.entity.ShangpinxinxiEntity;
import com.entity.StoreupEntity;
import com.entity.view.ShangpinxinxiView;
import com.entity.view.StoreupView;
import com.entity.vo.ShangpinxinxiVO;
import com.recommend.Recommend;
import com.recommend.UserSet;
import com.service.ShangpinxinxiService;
import com.utils.PageUtils;
import com.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("shangpinxinxiService")
public class ShangpinxinxiServiceImpl extends ServiceImpl<ShangpinxinxiDao, ShangpinxinxiEntity> implements ShangpinxinxiService {

	@Autowired
	private StoreupDao storeupDao;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<ShangpinxinxiEntity> page = this.selectPage(
				new Query<ShangpinxinxiEntity>(params).getPage(),
				new EntityWrapper<ShangpinxinxiEntity>()
		);
		return new PageUtils(page);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params, Wrapper<ShangpinxinxiEntity> wrapper) {
		Page<ShangpinxinxiView> page =new Query<ShangpinxinxiView>(params).getPage();
		page.setRecords(baseMapper.selectListView(page,wrapper));
		PageUtils pageUtil = new PageUtils(page);
		return pageUtil;
	}

	@Override
	public List<ShangpinxinxiEntity> recommend(Long currentUserId, Long currentGoodsId) {
		// 获取所有的用户-收藏商品信息
		List<StoreupView> storeupViews = storeupDao.selectListView(new EntityWrapper<StoreupEntity>().eq("tablename", "shangpinxinxi"));

		// 实例化商品信息集合对象
		List<ShangpinxinxiEntity> resultList = new ArrayList<>();

		// 如果所有的用户-收藏商品信息信息不等于空
		if (CollectionUtil.isNotEmpty(storeupViews)) {

			// 以用户id为键，商品信息集合为值的map集合对象
			Map<Long, List<StoreupView>> userShangpinxinxiMap = storeupViews.stream().collect(Collectors.groupingBy(StoreupView::getUserid));

			// 用户对象
			UserSet userSet = new UserSet();

			// 如果当前用户在map集合中没有匹配的key,也就是说该用户之前没有收藏过文章
			if (!userShangpinxinxiMap.containsKey(currentUserId)) {

				// 根据当前商品来id来查询被其他用户收藏的次数
				Long count = storeupDao.queryCount(currentGoodsId);

				// 将用户id设置到userSet对象中，在User对象中添加商品id及商品收藏的数量，将User用户信息添加到userSet对象的集合中
				userSet.put(currentUserId + "")
						.set(currentGoodsId, count).create();
			}

			// 循环
			userShangpinxinxiMap.forEach((k, v) -> {

				// 为UserSet中的User设置userName，k作为userName
				UserSet.User put = userSet.put(k + "");
				v.forEach(t -> {

					// 根据当前商品来id来查询被其他用户收藏的次数
					Long count = storeupDao.queryCount(currentGoodsId);

					// 在User对象中添加商品id及商品收藏的数量，将User用户信息添加到userSet对象的集合中
					put.set(t.getRefid(), count);
				});

				// 如果集合中的用户id等于当前传来的用户id
				if (k.intValue() == currentUserId) {

					// 根据当前商品id来查询被其他用户收藏的次数
					Long count = storeupDao.queryCount(currentGoodsId);

					userSet.put(currentUserId + "")
							.set(currentGoodsId, count);
				}

				// 将user对象循环添加到userSet集合中去
				put.create();
			});

			// 实例化推荐类
			Recommend recommend = new Recommend();

			// 根据当前用户id以及userSet推荐商品集合数据
			List<UserSet.RInfo> result = recommend.recommend(String.valueOf(currentUserId), userSet);

			// 存放商品ids
			List<Long> Ids = new ArrayList<>();

			// 循环遍历推荐的商品集合
			for (UserSet.RInfo zuopinxinxi : result) {
				Ids.add(zuopinxinxi.id);
			}

			// 如何推荐的商品ids不为空
			if (CollectionUtil.isNotEmpty(Ids)) {

				// 根据商品ids查询商品信息
				for (int i = 0; i < Ids.size(); i++) {
					resultList.add(baseMapper.selectById(Long.valueOf(String.valueOf(Ids.get(i)))));
				}
			}
		}
		// 过滤相同的商品信息
		List<ShangpinxinxiEntity> collect = resultList.stream().distinct().collect(Collectors.toList());

		// 返回集合数据
		return collect;
	}


	@Override
	public List<ShangpinxinxiVO> selectListVO(Wrapper<ShangpinxinxiEntity> wrapper) {
		return baseMapper.selectListVO(wrapper);
	}

	@Override
	public ShangpinxinxiVO selectVO(Wrapper<ShangpinxinxiEntity> wrapper) {
		return baseMapper.selectVO(wrapper);
	}

	@Override
	public List<ShangpinxinxiView> selectListView(Wrapper<ShangpinxinxiEntity> wrapper) {
		return baseMapper.selectListView(wrapper);
	}

	@Override
	public ShangpinxinxiView selectView(Wrapper<ShangpinxinxiEntity> wrapper) {
		return baseMapper.selectView(wrapper);
	}

}

